package blackstar.performance.logger.info;

import java.util.Stack;

/**
 * @author raywu (ayufox@gmail.com)
 * 
 */
public class LoadChainTree
{
	private Stack<LoadChainNode> executeStack = new Stack<LoadChainNode>();
	private LoadChainNode root;

	/**
	 * @param className
	 * @param methodName
	 */
	public void start(String className, String methodName, long time)
	{
		LoadChainNode node = new LoadChainNode(className, methodName);
		if (root == null)
		{
			root = node;
		}

		if (executeStack.size() > 0)
		{
			LoadChainNode lastNode = executeStack.peek();
			lastNode.addChild(node);
		}
		executeStack.push(node);
		node.start(time);
	}

	public void stop(long time)
	{
		if (executeStack.size() > 0)
		{
			LoadChainNode lastNode = executeStack.pop();
			if (lastNode != null)
			{
				lastNode.stop(time);
			}
		}
	}

	/**
	 * 
	 * @return LoadChainNode
	 */
	public LoadChainNode getRoot()
	{
		return this.root;
	}

	/**
	 * 
	 * @return boolean
	 */
	public boolean isFinish()
	{
		return (this.executeStack.size() == 0);
	}
}
